<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    一、通过reduce和filter方法将以下一维数组转换成树状图
</body>

</html>
<script>
    let a = [
        { id: 1, name: 'a', pid: 0 },
        { id: 2, name: 'aa', pid: 0 },
        { id: 3, name: 'aaa', pid: 1 },
        { id: 4, name: 'aaaa', pid: 2 },
        { id: 5, name: 'aaaaa', pid: 2 },
        { id: 6, name: 'aaaaaa', pid: 2 },
        { id: 7, name: 'aaaaaaa', pid: 1 },
        { id: 8, name: 'aaaaaaaa', pid: 3 },
        { id: 9, name: 'aaaaaaaaa', pid: 3 },
    ]
    let a1 = (arr, pid) => {
        return a.reduce((res, key) => {
            if (key["pid"] === pid) {
                key.children = a1(arr, key["id"])
                return res.concat(key)
            }
            return res;
        }, [])
    }
    console.log(a1(a, 0));
</script>